//
//  BuyTikcetInfoView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/23.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class BuyTikcetInfoView: UIView {
    
    // MARK: 订单数据
    public var restTicketInfo:TicketInfo? {
        didSet {
            updateLayout()
        }
    }
    
    let golayout = SubGoLayout()
    let retrunlayout = SubReturnLayout()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_view()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func init_view() -> Void {
        golayout.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        retrunlayout.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
    }
    
    override func layoutSubviews() {
        guard let ticket = restTicketInfo else {
            return
        }
        if ticket.arrival_date == nil {
            golayout.subTicket = ticket
            addSubview(golayout)
        }else {
            retrunlayout.subTicket = ticket
            addSubview(retrunlayout)
        }
    }
    
    class SubGoLayout: UIView {
        // 日期
        private let symbol = UILabel()
        // 舱位级别
        private let cabinname = UILabel()
        // 出发时间
        private let startTime = UILabel()
        // 到达时间
        private let stopTime = UILabel()
        // 飞行时长
        private let miletime = UILabel()
        // 出发航站楼
        private let departTime = UILabel()
        // 到达航站楼
        private let arrivalTime = UILabel()
        // 航班信息
        private let airLineInfo = UILabel()
        // 航空公司
        private let airLineName = UILabel()
        // 箭头图片
        private let arrowPic = UIImageView(image: UIImage(named: "checi_arrow"))
        
        public var subTicket:TicketInfo? {
            didSet {
                resetGoText(ticket: subTicket)
            }
        }
        
        private func resetGoText(ticket: TicketInfo?) -> Void {
            // 日期
            symbol.text = AirLineUtil.shareInstance.getConvertDate(time: ticket!.depart_date!, isYear: true)
            // 舱位级别
            cabinname.text = ticket!.cabinname
            // 出发时间
            startTime.text = ticket!.depart_time
            // 到达时间
            stopTime.text = ticket!.arrival_time
            // 飞行时长
            miletime.text = ticket!.miletime
            // 出发航站楼
            departTime.text = "\(ticket!.city!)机场 \(ticket!.depart_terminal!)"
            // 到达航站楼
            arrivalTime.text = "\(ticket!.endcity!)机场 \(ticket!.arrival_terminal!)"
            // 航班信息
            airLineInfo.text = "\(ticket!.flight_number!) | \(ticket!.craft!) | \(ticket!.food!) | 行李:\(ticket!.luggage!)KG | 里程:\(ticket!.mileage!)KM"
            // 航空公司
            airLineName.text = "该航班由\(ticket!.flight_name!)公司承运"
        }
        
        override func layoutSubviews() {
            // 日期
            symbol.textColor = COLOR_NICE
            symbol.font = UIFont.systemFont(ofSize: 12)
            symbol.frame = CGRect(x: MARGIN20, y: MARGIN , width: MARGIN100, height: MARGIN)
            addSubview(symbol)
            // 舱位级别
            cabinname.textAlignment = .right
            cabinname.textColor = COLOR_NICE
            cabinname.font = UIFont.systemFont(ofSize: 12)
            cabinname.frame = CGRect(x: frame.width - MARGIN100 - MARGIN20, y: MARGIN, width: MARGIN100, height: MARGIN)
            addSubview(cabinname)
            // 出发时间
            startTime.textColor = UIColor.black
            startTime.font = UIFont.boldSystemFont(ofSize: 25)
            startTime.frame = CGRect(x: MARGIN20, y: symbol.frame.maxY + MARGIN20 , width: MARGIN80, height: MARGIN20)
            addSubview(startTime)
            // 到达时间
            stopTime.textAlignment = .right
            stopTime.textColor = UIColor.black
            stopTime.font = UIFont.boldSystemFont(ofSize: 25)
            stopTime.frame = CGRect(x: frame.width - MARGIN100, y: symbol.frame.maxY + MARGIN20 , width: MARGIN80, height: MARGIN20)
            addSubview(stopTime)
            // 箭头图片
            arrowPic.frame.origin = CGPoint(x: (frame.width - arrowPic.frame.width) * 0.5, y: symbol.frame.maxY + MARGIN30 )
            addSubview(arrowPic)
            // 飞行时长
            miletime.textColor = UIColor.gray
            let mileTextWidth = miletime.text!.boundingRect(w: MARGIN50, h: MARGIN, fontSize: 13).width * 0.5
            let offset = arrowPic.bounds.size.width * 0.5 - mileTextWidth
            miletime.font = UIFont.systemFont(ofSize: 13)
            let miletimeX = (frame.width - arrowPic.frame.width) * 0.5 + offset
            miletime.frame = CGRect(x:miletimeX, y: arrowPic.frame.maxY - 18, width: MARGIN50, height: MARGIN)
            addSubview(miletime)
            // 出发航站楼
            departTime.textColor = COLOR_NICE
            departTime.font = UIFont.systemFont(ofSize: 12)
            departTime.frame = CGRect(x: MARGIN20, y: startTime.frame.maxY + MARGIN10 , width: MARGIN140, height: MARGIN)
            addSubview(departTime)
            // 到达航站楼
            arrivalTime.textAlignment = .right
            arrivalTime.textColor = COLOR_NICE
            arrivalTime.font = UIFont.systemFont(ofSize: 12)
            arrivalTime.frame = CGRect(x: frame.width - MARGIN140 - MARGIN20, y: startTime.frame.maxY + MARGIN10 , width: MARGIN140, height: MARGIN)
            addSubview(arrivalTime)
            // 航班信息
            airLineInfo.textColor = COLOR_NICE
            airLineInfo.font = UIFont.systemFont(ofSize: 12)
            airLineInfo.frame = CGRect(x: MARGIN20, y: departTime.frame.maxY + MARGIN20 , width: SCREEN_WIDTH - MARGIN40, height: MARGIN)
            addSubview(airLineInfo)
            // 航空公司
            let airLineName = UILabel()
            airLineName.textColor = COLOR_NICE
            airLineName.font = UIFont.systemFont(ofSize: 12)
            airLineName.frame = CGRect(x: MARGIN20, y: airLineInfo.frame.maxY + MARGIN10 , width: SCREEN_WIDTH - MARGIN40, height: MARGIN)
            addSubview(airLineName)
        }
    }
    
    class SubReturnLayout: UIView {
        // 去程
        private let goLabel = UILabel(title: "去程", color: UIColor.gray, size: 12, align: .center)
        // 去程日期
        private let goDateLabel = UILabel(title: "", color: UIColor.black)
        // 去程时间
        private let goTimeLabel = UILabel(title: "", color: UIColor.black)
        // 去程机场信息1
        private let goFlightLabel = UILabel(title: "", color: UIColor.gray,size: 12)
        // 返程日期
        private let returnDateLabel = UILabel(title: "", color: UIColor.black)
        // 返程时间
        private let returnTimeLabel = UILabel(title: "", color: UIColor.black)
        // 返程机场信息1
        private let returnFlightLabel = UILabel(title: "", color: UIColor.gray,size: 12)
        // 返程机场信息2
        private let returnFlightLabel2 = UILabel(title: "", color: UIColor.gray,size: 12)
        
        public var subTicket:TicketInfo? {
            didSet {
                resetRetrunText(ticket: subTicket)
            }
        }
        
        private func resetRetrunText(ticket:TicketInfo?) -> Void {
            // 去程日期
            goDateLabel.text = AirLineUtil.shareInstance.getConvertDate(time: ticket!.depart_date!, isYear: true)
            // 去程时间
            goTimeLabel.text = "\(ticket!.depart_time!) - \(ticket!.arrival_time!)"
            // 去程机场信息1
            goFlightLabel.text = "\(ticket!.city!)机场\(ticket!.depart_terminal!) - \(ticket!.endcity!)机场\(ticket!.arrival_terminal!)"
            // 返程日期
            returnDateLabel.text = AirLineUtil.shareInstance.getConvertDate(time: ticket!.arrival_date!, isYear: true)
            // 返程时间
            returnTimeLabel.text = "\(ticket!.arrival_time!) - \(ticket!.depart_time!)"
            // 返程机场信息1
            returnFlightLabel.text = "\(ticket!.endcity!)机场\(ticket!.arrival_terminal!) - \(ticket!.city!)机场\(ticket!.depart_terminal!)"
            // 返程机场信息2
            returnFlightLabel2.text = "\(ticket!.flight_number!) | \(ticket!.craft!) | \(ticket!.food!) | 行李:\(ticket!.luggage!)KG | 里程:\(ticket!.mileage!)KM"
        }
        
        override func layoutSubviews() {
            // 去程
            goLabel.layer.borderColor = UIColor.gray.cgColor
            goLabel.layer.borderWidth = 1
            goLabel.layer.cornerRadius = 3
            goLabel.frame = CGRect(x: MARGIN10, y: MARGIN, width: MARGIN40, height: MARGIN20)
            addSubview(goLabel)
            // 去程日期 位置
            let godateX = goLabel.frame.maxX + MARGIN10
            goDateLabel.font = UIFont.boldSystemFont(ofSize: 15)
            goDateLabel.frame = CGRect(x: godateX, y: MARGIN, width: MARGIN130, height: MARGIN20)
            addSubview(goDateLabel)
            // 去程时间 位置
            let gotimeX = goDateLabel.frame.maxX + MARGIN10
            goTimeLabel.font = UIFont.boldSystemFont(ofSize: 15)
            goTimeLabel.frame = CGRect(x: gotimeX, y: MARGIN, width: frame.width - gotimeX - MARGIN10, height: MARGIN20)
            addSubview(goTimeLabel)
            // 去程机场信息1 位置
            let goflightY = goLabel.frame.maxY + MARGIN5
            let goflightW = frame.width - godateX - MARGIN10
            goFlightLabel.frame = CGRect(x: godateX, y: goflightY, width: goflightW, height: MARGIN20)
            addSubview(goFlightLabel)
            // 返程
            let returnLabel = UILabel(title: "返程", color: UIColor.gray, size: 12, align: .center)
            returnLabel.layer.borderColor = UIColor.gray.cgColor
            returnLabel.layer.borderWidth = 1
            returnLabel.layer.cornerRadius = 3
            let returnY = goFlightLabel.frame.maxY + MARGIN20
            returnLabel.frame = CGRect(x: MARGIN10, y: returnY, width: MARGIN40, height: MARGIN20)
            addSubview(returnLabel)
            // 返程日期 位置
            returnDateLabel.font = UIFont.boldSystemFont(ofSize: 15)
            returnDateLabel.frame = CGRect(x: godateX, y: returnY, width: MARGIN130, height: MARGIN20)
            addSubview(returnDateLabel)
            // 返程时间 位置
            returnTimeLabel.font = UIFont.boldSystemFont(ofSize: 15)
            returnTimeLabel.frame = CGRect(x: gotimeX, y: returnY, width: frame.width - gotimeX - MARGIN10, height: MARGIN20)
            addSubview(returnTimeLabel)
            // 返程机场信息1 位置
            let returnflightY = returnDateLabel.frame.maxY + MARGIN5
            returnFlightLabel.frame = CGRect(x: godateX, y: returnflightY, width: goflightW, height: MARGIN20)
            addSubview(returnFlightLabel)
            // 返程机场信息2 位置
            let returnF2Y = returnFlightLabel.frame.maxY + MARGIN20
            returnFlightLabel2.frame = CGRect(x: MARGIN10, y: returnF2Y, width: frame.width - MARGIN20, height: MARGIN20)
            addSubview(returnFlightLabel2)
        }
    }
}
